package com.enhe.gck.fg;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.enhe.gck.CarbonBaseConstant;
import com.enhe.gck.CarbonRelationType;
import com.enhe.gck.communal.*;
import com.enhe.gck.item.*;
import com.enhe.gck.pojo.*;
import com.enhe.gck.utils.PDFUtil;
import com.kuangkie.carbon.common.BytesInfoVO;
import com.kuangkie.carbon.common.IntegrationMsg;
import com.kuangkie.carbon.common.RemoveDTO;
import com.kuangkie.carbon.common.RemoveRecordInfo;
import com.kuangkie.carbon.fg.*;
import com.kuangkie.carbon.fg.ops.FGOpsComplexus;
import com.kuangkie.carbon.fg.ops.ProRecordOps;
import com.kuangkie.carbon.fg.ops.ProRecordOpsBuilder;
import com.kuangkie.carbon.panel.CarbonPanel;
import com.kuangkie.carbon.record.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import com.enhe.gck.CarbonEnumKeyValue; //枚举字典信息

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.awt.SystemColor.info;

@Slf4j
@Repository(value = "hwane16829366042330866425862")
public class SupplyOrderHWANE16829BNB implements FirstRoundImproveFuncGroup,SecondRoundImproveFuncGroup, FetchFuncGroup ,RemoveFuncGroup,FuseCallBackFuncGroup{
	@Resource
	private IcCardHWANE16640BNB icCardHWANE16640BNB;

	@Resource
	private FileTemplateHWANE18473BNB fileTemplateHWANE18473BNB;
	@Override
	public void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
			ImproveResultFactory improveResultFactory) {
		SupplyOrder order=proRecord.getBean(SupplyOrder.class);
		if(PublicMethod.verifyIsStopCustomer(proRecord,improveResultFactory,PublicConstant.SM)){
			return;
		}

		ProRecordOpsBuilder setData=improveResultFactory.getCurrentProRecordOpsBuilder();
		if(proRecord.isNew()||(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRefund())
				&&String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsCancel()))){
			if(proRecord.isNew()){
				//配置公用数据
				order.setOrder(PublicMethod.getOrderCodeSM());
				setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_单号,order.getOrder());
				setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f);
				setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f);
			}
			//验证重量 处理重量
			if(!verifyWeight(order,proRecord,improveResultFactory)){
				return;
			}

			setFinishTime(order,improveResultFactory);

			if(computeLiter(order,improveResultFactory)){
				return;
			}
			//额外命令
			if(String.valueOf(CarbonEnumKeyValue.操作命令_订单_手工补录_ddsgbl).equals(order.getCommand())){
				inputHand(proRecord,improveResultFactory,order.getOrder());
				return;
			}

			boolean flow1=ICCar(proRecord,improveResultFactory);
			if(!flow1){
				return;
			}
			initialization(proRecord,improveResultFactory);
		}else {
//			deductionRefund(proRecord);
		}
	}

	/**
	 * IC卡逻辑 改为待过一磅
	 */
	private boolean ICCar(ProRecord proRecord,ImproveResultFactory improveResultFactory){
		if(String.valueOf(PublicConstant.OrderStateOver).equals(proRecord.getString(SupplyOrderHWANE16829Item.基本属性组_状态))){
			return true;
		}

		boolean isOk=true;
		if(StrUtil.isNotBlank(proRecord.getString(SupplyOrderHWANE16829Item.基本属性组_IC卡号))){
			isOk =icCardHWANE16640BNB.verifyCardNumber(improveResultFactory,proRecord.getRecordCode(),proRecord.getString(SupplyOrderHWANE16829Item.基本属性组_IC卡号));
			if(isOk){
				if(proRecord.isNew()){
					improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(SupplyOrderHWANE16829Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb);
				}else {
					SupplyOrder orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getRecordBean(proRecord.getRecordCode(), SupplyOrder.class);
					if(orderOld!=null){
						if(String.valueOf(CarbonEnumKeyValue.订单记录_状态_已创建_ycj).equals(orderOld.getState())){
							improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(SupplyOrderHWANE16829Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb);
						}
					}
				}
			}
		}
		return isOk;
	}
	/**
	 * 初始化数据
	 */
	private void initialization(ProRecord proRecord, ImproveResultFactory improveResultFactory){
		if(proRecord.isNew()){
			Material material=CarbonPanel.getRecordDiscover(CarbonBaseConstant.物料管理_wlgl).getRecordBean(proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_物料名_物料管理), Material.class);
			improveResultFactory
					.getCurrentProRecordOpsBuilder()
					.putAttribute(SupplyOrderHWANE16829Item.基本属性组_8位取货码,PublicMethod.getPickupCode())
					.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否扣重,false)
					.putAttribute(SupplyOrderHWANE16829Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_已创建_ycj)
					.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否已开票,false)
					.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否已打印小票,false)
					.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否化验, !material.getIsNeedTest())
					.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否验收, !material.getIsNeedCheck())
					.putAttribute(SupplyOrderHWANE16829Item.基本属性组_录入类型,CarbonEnumKeyValue.订单记录_录入类型_系统下单_xtxd)
			;
		}
	}

	/**
	 * 设置完成时间
	 */
	private void setFinishTime(SupplyOrder order,ImproveResultFactory improveResultFactory){
		ProRecordOpsBuilder setData=improveResultFactory.getCurrentProRecordOpsBuilder();
		log.info("设置完成时间");
		if(order.getFinishTime()==null){
			if(String.valueOf(CarbonEnumKeyValue.订单记录_状态_已出厂_ycc).equals(order.getState())){
				SupplyOrder orderOrigin=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getRecordBean(order.getRecordCode(),SupplyOrder.class);
				if(orderOrigin!=null){
					if(!String.valueOf(CarbonEnumKeyValue.订单记录_状态_已出厂_ycc).equals(orderOrigin.getState())){
						log.info("未完成过，设置时间");
						setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_完成时间, LocalDateTime.now());
					}else {
						if(orderOrigin.getFinishTime()==null){
							log.info("完成过，未设置时间");
							setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_完成时间, LocalDateTime.now());
						}
					}
				}
			}
		}
	}

	/**
	 * 计算升并赋值
	 */
	@Deprecated
	private void computeLiterOld(SupplyOrder order,ProRecord proRecord,ImproveResultFactory improveResultFactory,boolean isAdditional){
		if(isAdditional){
			Material material=PublicMethod.getInfo(CarbonBaseConstant.物料管理_wlgl,proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_物料名_物料管理),Material.class);
			if(material!=null&&material.getDensity()!=null&&material.getDensity().compareTo(BigDecimal.ZERO)>0){
				PublicMethod.saveAddNow(improveResultFactory).putAttribute(SupplyOrderHWANE16829Item.基本属性组_容积_升_,PublicMethod.getLiter(new BigDecimal(String.valueOf(order.getActualWeight())),material.getDensity()));
			}
		}else {
			if(String.valueOf(PublicConstant.OrderStateOver).equals(order.getState())){
				SupplyOrder orderOld=PublicMethod.getInfo(CarbonBaseConstant.供货订单_ghdd,order.getRecordCode(),SupplyOrder.class,2);
				if(orderOld!=null){
					if(!String.valueOf(PublicConstant.OrderStateOver).equals(orderOld.getState())){
						if(orderOld.getMaterial()!=null&&orderOld.getMaterial().getDensity()!=null&&orderOld.getMaterial().getDensity().compareTo(BigDecimal.ZERO)>0){
							PublicMethod.saveAddNow(improveResultFactory).putAttribute(SupplyOrderHWANE16829Item.基本属性组_容积_升_,PublicMethod.getLiter(new BigDecimal(String.valueOf(order.getActualWeight())),orderOld.getMaterial().getDensity()));
						}
					}
				}
			}
		}
	}
	private boolean computeLiter(SupplyOrder order,ImproveResultFactory improveResultFactory){
		if(order.getIsToLiter()!=null&&order.getIsToLiter()&&order.getActualWeight()!=null){
			if(order.getDensity()!=null&&order.getDensity().compareTo(BigDecimal.ZERO)>0){
				PublicMethod.saveAddNow(improveResultFactory).putAttribute(SupplyOrderHWANE16829Item.基本属性组_容积_升_,PublicMethod.getLiter(new BigDecimal(String.valueOf(order.getActualWeight())),order.getDensity()));
			}else {
				PublicMethod.returnMessage(improveResultFactory, PublicReturn.SUPPLY_ORDER_3);
				return true;
			}
		}else {
			if(order.getLiter()!=null){
				PublicMethod.saveAddNow(improveResultFactory).putAttribute(SupplyOrderHWANE16829Item.基本属性组_容积_升_,null);
			}
		}
		return false;
	}

	/**
	 * 生成两联表
	 */
	private boolean saveFile(ProRecord fGRelation){
		try {
			log.info("生成两联表:{}",fGRelation.toJson());
			Customer date1=CarbonPanel.getRecordDiscover(CarbonBaseConstant.客户管理_khgl).getRecordBean(fGRelation.getGuestRecordCode(CarbonRelationType.RR_供货订单_客户名称_客户管理), Customer.class);
			Car date2=CarbonPanel.getRecordDiscover(CarbonBaseConstant.车辆管理_clgl).getRecordBean(fGRelation.getGuestRecordCode(CarbonRelationType.RR_供货订单_车牌号_车辆管理), Car.class);
			//无车辆信息暂不生成两联表
			if(date2==null){
				return false;
			}

			Material date3=CarbonPanel.getRecordDiscover(CarbonBaseConstant.物料管理_wlgl).getRecordBean(fGRelation.getGuestRecordCode(CarbonRelationType.RR_供货订单_物料名_物料管理), Material.class);
//		Customer date4=CarbonPanel.getRecordDiscover(CarbonBaseConstant.客户管理_khgl).getRecordBean(fGRelation.getGuestRecordCode(CarbonRelationType.RR_供货订单_供货订单的运输商_客户管理), Customer.class);
			Map<String,Object> data = new HashMap<>();
			String data12=PublicMethod.getDateToString(LocalDate.now(), PublicEnum.DateString.date2.getCode());
			data.put("data11",fGRelation.getString(SupplyOrderHWANE16829Item.基本属性组_单号));data.put("data12",data12);
			data.put("data21",date1==null?"":date1.getName());data.put("data22",date2==null?"":date2.getCarNo());
			data.put("data31",date3==null?"":date3.getName());data.put("data32",fGRelation.getString(SupplyOrderHWANE16829Item.基本属性组_8位取货码));
			PublicEntity.Barcode barcode=new PublicEntity.Barcode();
			barcode.setCode(fGRelation.getString(SupplyOrderHWANE16829Item.基本属性组_单号));
			barcode.setX(30);
			barcode.setY(55);
			barcode.setFlagReverseY(true);
			log.info("打印信息:{}",JSON.toJSON(data));

			FGRecord inputStreamF=fileTemplateHWANE18473BNB.getInfo(CarbonEnumKeyValue.文件模板_供货两联表PDF模板_ghllbmb);
			if(inputStreamF==null){
				log.info("无文件模板_供货两联表PDF模板_ghllbmb");
				return false;
			}

			BytesInfoVO file=new BytesInfoVO("tableau.pdf",PDFUtil.getBytes(fileTemplateHWANE18473BNB.getInfoFile(inputStreamF),data,barcode));
			FGRecordComplexusBuilder fGRecordComplexusBuilder=CarbonPanel.getFGRecordComplexusBuilder();
			fGRecordComplexusBuilder.getProRecordBuilder(CarbonBaseConstant.文件_wj)
					.addRelation(CarbonRelationType.RR_文件_属于供货订单_供货订单,fGRelation.getRecordCode())
					.putAttribute(FileHWANE18503Item.基本属性组_文件,file);
			CarbonPanel.getRecordIntegration(CarbonBaseConstant.文件_wj).integrate(fGRecordComplexusBuilder.getFGRecordComplexus());
			return true;
		}catch (Exception e){
			log.info("异常,{}",e.getMessage());
			return false;
		}
	}

//	@Deprecated
//	private synchronized boolean verifyWeight(SupplyOrder order,ProRecord proRecord, ImproveResultFactory improveResultFactory){
//		log.info("验证重量开始:{}",JSON.toJSON(order));
//		String planId=proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_供货计划_供货计划);
//		if(!StrUtil.isBlank(planId)){
//			SupplyOrderBig supplyOrderBig=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货计划_ghjh).getRecordBean(planId, SupplyOrderBig.class);
//			if(supplyOrderBig!=null){
//				BigDecimal sumWeight=PublicMethod.addBigDecimal(supplyOrderBig.getWeightExpect(),order.getWeight());
//				if(proRecord.isNew()){
//					if(supplyOrderBig.getWeight()!=null&&PublicMethod.compareToBigDecimal(supplyOrderBig.getWeight(),sumWeight)>=0){
//						CarbonPanel.getFGRecordBuilder(CarbonBaseConstant.供货计划_ghjh,planId).putAttribute(SupplyOrderBigHWANE18255Item.基本属性组_预计供重,sumWeight).integrate();
//						return true;
//					}else {
//						PublicMethod.returnMessage(improveResultFactory,PublicReturn.SUPPLY_ORDER_1);
//						return false;
//					}
//				}else {
//					SupplyOrder supplyOrderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getRecordBean(order.getRecordCode(), SupplyOrder.class);
//					//编辑处理
//					if(String.valueOf(PublicConstant.OrderStateOver).equals(order.getState())){
//						return true;
//					}else {
//						sumWeight=PublicMethod.subtractBigDecimal(sumWeight,supplyOrderOld.getWeight());
//						if(supplyOrderBig.getWeight()!=null&&PublicMethod.compareToBigDecimal(supplyOrderBig.getWeight(),sumWeight)>=0){
//							return true;
//						}else {
//							PublicMethod.returnMessage(improveResultFactory,PublicReturn.SUPPLY_ORDER_1);
//							return false;
//						}
//					}
//				}
//			}
//		}
//		return false;
//	}

	private synchronized boolean verifyWeight(SupplyOrder order,ProRecord proRecord, ImproveResultFactory improveResultFactory){
		log.info("验证重量开始:{}",JSON.toJSON(order));
		String planId=proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_供货计划_供货计划);
		if(!StrUtil.isBlank(planId)){
			SupplyOrderBig supplyOrderBig=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货计划_ghjh).getRecordBean(planId, SupplyOrderBig.class);
			if(supplyOrderBig!=null){
				if(proRecord.isNew()){
					if(PublicMethod.compareToBigDecimal(supplyOrderBig.getWeightOk(),supplyOrderBig.getWeight())>=0){
						PublicMethod.returnMessage(improveResultFactory,PublicReturn.SUPPLY_ORDER_1);
						return false;
					}
				}
			}
		}
		return true;
	}

	/**
	 * 手工补录
	 */
	public boolean inputHand(ProRecord proRecord,ImproveResultFactory improveResultFactory,String code) {
		log.info("手工补录业务开始:{}",proRecord.toJson());
		ProRecordOpsBuilder setData=improveResultFactory.getCurrentProRecordOpsBuilder();
		SupplyOrder order=proRecord.getBean(SupplyOrder.class);
		if(StringUtils.isEmpty(code)){
			code=order.getOrder();
		}
		if(PublicMethod.compareToBigDecimal(order.getWeight2(),order.getWeight1())>0){
			PublicMethod.returnMessage(improveResultFactory,PublicReturn.SUPPLY_ORDER_2);
			return false;
		}

		setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_实际重量,PublicMethod.subtractBigDecimal(order.getWeight1(),order.getWeight2()));
		setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_已出厂_ycc);
		setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否扣重,false);
		setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否已打印小票,true);
		setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否已开票,false);
		setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_录入类型,CarbonEnumKeyValue.订单记录_录入类型_人工补录_rgbl);
		setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否化验,true);
		setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否验收,true);
//		computeLiter(order,proRecord,improveResultFactory,true);
		log.info("补录地磅信息开始");
		PublicMethod.saveWeigh(improveResultFactory,code,BigDecimal.ZERO,BigDecimal.valueOf(order.getWeight1()),BigDecimal.ZERO,1,order.getWeight1Time());
		PublicMethod.saveWeigh(improveResultFactory,code,BigDecimal.valueOf(order.getWeight2()),BigDecimal.ZERO,BigDecimal.ZERO,2,order.getWeight2Time());
		log.info("补录地磅信息结束");
		log.info("手工补录业务结束:");
		return true;
	}

	/**
	 * 触发扣重回退
	 */
//	@Deprecated
//	private void deductionRefund(ProRecord proRecord){
//		SupplyOrder orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getRecordBean(proRecord.getRecordCode(),SupplyOrder.class);
//		String ghjh=proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_供货计划_供货计划);
//		if(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(orderOld.getIsRefund())
//				&&String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(orderOld.getIsCancel())){
//			log.info("QAQ扣重回退开始:{}",proRecord.toJson());
//			SupplyOrderBig supplyOrderBigOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货计划_ghjh).getRecordBean(ghjh,SupplyOrderBig.class);
//			if(String.valueOf(PublicConstant.OrderStateOver).equals(orderOld.getState())){
//				CarbonPanel.getFGRecordBuilder(CarbonBaseConstant.供货计划_ghjh,ghjh)
//						.putAttribute(SupplyOrderBigHWANE18255Item.基本属性组_已供重量,PublicMethod.subtractBigDecimal(supplyOrderBigOld.getWeightOk(),orderOld.getActualWeight()))
//						.integrate();
//			}else {
//				CarbonPanel.getFGRecordBuilder(CarbonBaseConstant.供货计划_ghjh,ghjh)
//						.putAttribute(SupplyOrderBigHWANE18255Item.基本属性组_预计供重,PublicMethod.subtractBigDecimal(supplyOrderBigOld.getWeightExpect(),orderOld.getWeight()))
//						.integrate();
//			}
//			log.info("QAQ扣重结束");
//		}
//	}

	/**
	 * 扣重
	 * @param id 计算id
	 * @param haveId 是否计算当前id
	 */
	private synchronized boolean kouWeight(String id,boolean haveId){
		log.info("QAQ处理扣重开始:{}",id);
//		BigDecimal yuJi=BigDecimal.ZERO;
		BigDecimal shiJi=BigDecimal.ZERO;
		String planId= CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getRelationCorrelation(id).getGuestRecordCode(CarbonRelationType.RR_供货订单_供货计划_供货计划);
		if(!StrUtil.isBlank(planId)){
			RecordQueryProConJunctionFactory lj=CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.供货订单_ghdd).getProConJunctionFactory()
					.addEqual(SupplyOrderHWANE16829Item.基本属性组_是否作废,false)
					.addEqual(SupplyOrderHWANE16829Item.基本属性组_是否退货,false);
			lj.getRelationCriterionFactory(CarbonBaseConstant.供货计划_ghjh)
					.setInRelationTypes(CarbonRelationType.RR_供货订单_供货计划_供货计划)
					.setInGuestCodes(planId);
			List<SupplyOrder> list=lj.goBack().queryBeans(SupplyOrder.class);
			if(list!=null&& !list.isEmpty()){
				if(haveId){
					for (SupplyOrder x : list) {
						if(String.valueOf(PublicConstant.OrderStateOver).equals(x.getState())){
							shiJi=PublicMethod.addBigDecimal(shiJi,x.getActualWeight());
//							yuJi=PublicMethod.addBigDecimal(yuJi,x.getActualWeight());
						}else {
//							yuJi=PublicMethod.addBigDecimal(yuJi,x.getWeight());
						}
					}
				}else {
					for (SupplyOrder x : list) {
						if(!id.equals(x.getRecordCode())){
							if(String.valueOf(PublicConstant.OrderStateOver).equals(x.getState())){
								shiJi=PublicMethod.addBigDecimal(shiJi,x.getActualWeight());
//								yuJi=PublicMethod.addBigDecimal(yuJi,x.getActualWeight());
							}else {
//								yuJi=PublicMethod.addBigDecimal(yuJi,x.getWeight());
							}
						}
					}
				}
			}

			log.info("QAQ修改扣重开始:{},实际{}",planId,shiJi);
			IntegrationMsg msg=CarbonPanel.getFGRecordBuilder(CarbonBaseConstant.供货计划_ghjh,planId)
//					.putAttribute(SupplyOrderBigHWANE18255Item.基本属性组_预计供重,yuJi)
					.putAttribute(SupplyOrderBigHWANE18255Item.基本属性组_已供重量,shiJi)
					.integrate();
			return msg.success();
		}
		return true;
	}

	@Override
	public void fetchImprove(FuncGroupContext funcGroupContext, FGRecord fgRecord, FetchResultFactory fetchResultFactory) {
		SupplyOrder order=fgRecord.getBean(SupplyOrder.class);
		try {
			WeighOther weighOther =PublicMethod.getWeigh(order.getOrder(),1);
			if(weighOther !=null){
				PublicMethod.setPmWeightSub(order.getState(),fetchResultFactory, weighOther,order.getActualWeight(),SupplyOrderHWANE16829Item.基本属性组_皮重,SupplyOrderHWANE16829Item.基本属性组_毛重);
			}

			try {
				if(weighOther !=null){
					ProRecordOpsBuilder setData=fetchResultFactory.getRecordOpsBuilder();
					setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_过磅1时间,weighOther.getPassTime());
//					setData.putFileAttribute(SupplyOrderHWANE16829Item.基本属性组_过磅图片,"11.png",weighOther.getCapturePic().getBody(),"png",null);
//					setData.putFileAttribute(SupplyOrderHWANE16829Item.基本属性组_过1磅图片2,"12.png",weighOther.getCapturePic2().getBody(),"png",null);
//					setData.putFileAttribute(SupplyOrderHWANE16829Item.基本属性组_过1磅图片3,"13.png",weighOther.getCapturePic3().getBody(),"png",null);
					WeighOther weighOther2 =PublicMethod.getWeigh(order.getOrder(),2);
					if(weighOther2 !=null){
						setData.putAttribute(SupplyOrderHWANE16829Item.基本属性组_过磅2时间,weighOther2.getPassTime());
//						setData.putFileAttribute(SupplyOrderHWANE16829Item.基本属性组_过2磅图片1,"21.png",weighOther2.getCapturePic().getBody(),"png",null);
//						setData.putFileAttribute(SupplyOrderHWANE16829Item.基本属性组_过2磅图片2,"22.png",weighOther2.getCapturePic2().getBody(),"png",null);
//						setData.putFileAttribute(SupplyOrderHWANE16829Item.基本属性组_过2磅图片3,"23.png",weighOther2.getCapturePic3().getBody(),"png",null);
					}
				}
			}catch ( Exception e){}
		}catch (Exception e){
			log.info("数据处理异常:{}",JSON.toJSON(order));
		}

//		BigDecimal weightTare=BigDecimal.ZERO;
//		BigDecimal weightGross=BigDecimal.ZERO;
//		Weigh weigh=PublicMethod.getWeigh(order.getOrder(),1);
//		if(weigh!=null){
//			weightGross=weigh.getWeightGross()==null?BigDecimal.ZERO:BigDecimal.valueOf(weigh.getWeightGross());
//			Weigh weigh2=PublicMethod.getWeigh(order.getOrder(),2);
//			if(weigh2!=null){
//				weightTare=order.getActualWeight()==null?BigDecimal.ZERO:PublicMethod.subtractBigDecimal(weightGross,order.getActualWeight());
//			}
//		}
//		fetchResultFactory.getRecordOpsBuilder()
//				.putAttribute(SupplyOrderHWANE16829Item.基本属性组_皮重,weightTare)
//				.putAttribute(SupplyOrderHWANE16829Item.基本属性组_毛重,weightGross)
//		;
	}

	@Override
	public boolean beforeRemove(FuncGroupContext funcGroupContext, RemoveRecordInfo removeRecordInfo, DeleteMessageFactory defDeleteMessageFactory) {
		Collection<String> list=removeRecordInfo.getRecordCodes();
		log.info("删除业务开始:{}",list);
		boolean isOk=true;
		if(list!=null){
			for (String x : list) {
				FGRecord info=CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.供货订单_ghdd).getProConJunctionFactory()
						.addEqual(CarbonBaseConstant.COLUMN_ABP0001,x)
						.addEqual(SupplyOrderHWANE16829Item.基本属性组_状态,PublicConstant.OrderStateOver)
						.addEqual(SupplyOrderHWANE16829Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f)
						.addEqual(SupplyOrderHWANE16829Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f)
						.goBack().queryARecord();
				if(info!=null){
					PublicMethod.returnMessage(defDeleteMessageFactory, PublicReturn.DELETE_1);
					isOk=false;
				}else {
//					ProRecord proRecord=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getProRecord(x);
//					deductionRefund(proRecord);
				}

				kouWeight(x,false);
			}
		}
		log.info("删除业务结束");
		return isOk;
	}

	@Override
	public boolean afterRemove(FuncGroupContext funcGroupContext, RemoveRecordInfo removeRecordInfo, DeleteMessageFactory defDeleteMessageFactory) {
		return true;
	}

	@Override
	public boolean afterFusition(FuncGroupContext funcGroupContext, FuseContext fuseContext, String s) {
		ProRecord order=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getProRecord(s);
		if(order!=null){
			if(order.getBoolean(SupplyOrderHWANE16829Item.基本属性组_是否生成两联表)==null){
				if(saveFile(order)){
					CarbonPanel.getFGRecordBuilder(CarbonBaseConstant.供货订单_ghdd,s).putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否生成两联表,true).integrate();
				}
			}else {
				if(!order.getBoolean(SupplyOrderHWANE16829Item.基本属性组_是否生成两联表)){
					if(saveFile(order)){
						CarbonPanel.getFGRecordBuilder(CarbonBaseConstant.供货订单_ghdd,s).putAttribute(SupplyOrderHWANE16829Item.基本属性组_是否生成两联表,true).integrate();
					}
				}
			}
		}

		kouWeight(s,true);
		return true;
	}

	@Override
	public void preImprove(FuncGroupContext funcGroupContext, ProRecord proRecord, ProRecordOps proRecordOps, FGRecordComplexus fgRecordComplexus, FGOpsComplexus fgOpsComplexus, ImproveResultFactory improveResultFactory) {

	}

	@Override
	public void improve(FuncGroupContext funcGroupContext, ProRecord proRecord, FGRecordComplexus fgRecordComplexus, ImproveResultFactory improveResultFactory) {
		SupplyOrder order=proRecord.getBean(SupplyOrder.class);
		if(proRecord.isNew()){
			OperationLogHWANE21797BNB.save(improveResultFactory,5,1,proRecord.getRecordCode());
			String ghOrder =proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_供货计划_供货计划);
			if(StringUtils.isBlank(ghOrder)){
				PublicMethod.returnMessage(improveResultFactory, PublicReturn.ORDER_10);
				return;
			}
			FGRelationCorrelation ghOrderRelation=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货计划_ghjh).getRelationCorrelation(ghOrder);
			String ghs =ghOrderRelation.getGuestRecordCode(CarbonRelationType.RR_供货计划_供货商_客户管理);
			String yl =ghOrderRelation.getGuestRecordCode(CarbonRelationType.RR_供货计划_原料_物料管理);
			if(StringUtils.isBlank(yl)||StringUtils.isBlank(ghs)){
				PublicMethod.returnMessage(improveResultFactory, PublicReturn.ORDER_10);
				return;
			}
			PublicMethod.saveAddNow(improveResultFactory)
					.addRelation(CarbonRelationType.RR_供货订单_客户名称_客户管理,ghs)
					.addRelation(CarbonRelationType.RR_供货订单_物料名_物料管理,yl);

			if(saveBatch(order,proRecord,improveResultFactory,ghs,yl)){
				return;
			}
		}else {
			if(String.valueOf(CarbonEnumKeyValue.操作命令_手动还卡_sdhk).equals(order.getCommand())){
				SupplyOrder orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getRecordBean(order.getRecordCode(),SupplyOrder.class,1);
				if(orderOld!=null){
					if(String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(orderOld.getState())){
						OperationLogHWANE21797BNB.save(improveResultFactory,5,7,proRecord.getRecordCode());
						improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(SupplyOrderHWANE16829Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_已出厂_ycc);
					}else {
						PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_27);
						return;
					}
				}
			}

			if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsCancel())||
					String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRefund())
			){
				SupplyOrder orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.供货订单_ghdd).getRecordBean(proRecord.getRecordCode(),SupplyOrder.class,1);
				if(orderOld!=null){
					if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsCancel())){
						if(!(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(orderOld.getIsCancel()))){
							OperationLogHWANE21797BNB.save(improveResultFactory,5,3,proRecord.getRecordCode());
						}
					}
					if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRefund())){
						if(!(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(orderOld.getIsRefund()))){
							OperationLogHWANE21797BNB.save(improveResultFactory,5,4,proRecord.getRecordCode());
						}
					}
				}
			}
		}
	}

	private boolean saveBatch(SupplyOrder order,ProRecord proRecord,ImproveResultFactory improveResultFactory,String ghs,String yl){
		if(order.getBatch()!=null&&order.getBatch()){
			Integer saveNumber=order.getSaveNumber();
			Collection<String> listCarId=proRecord.getGuestRecordCodes(CarbonRelationType.RR_供货订单_多个车辆_车辆管理);
			Integer saveCar=listCarId==null?0:listCarId.size();
			if(saveNumber==null||saveNumber<1||saveCar<1){
				log.info("车辆单数为空或车数为空");
				return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_30);
			}
			for (int i = 0; i < saveCar; i++) {
				for (int j = 0; j < saveNumber; j++) {
					SupplyOrder info=new SupplyOrder();
					info.setType(order.getType());
					info.setOrderSource(order.getOrderSource());
					info.setInputType(order.getInputType());
					info.setActualWeight(0.0);

					info.setReceiptPrintedFlag(false);
					info.setReceiptPrintedNumber(0);
					info.setInvoicedFlag(false);

					info.setCustomerId(Long.valueOf(ghs));
					info.setMaterialId(Long.valueOf(yl));
					if(proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_供货计划_供货计划)!=null){
						info.setSupplyOrderBigId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_供货计划_供货计划)));
					}
					if(proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_司机_司机管理)!=null){
						info.setDriverId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_供货订单_司机_司机管理)));
					}
					info.setCarId(Long.valueOf(listCarId.toArray()[i].toString()));
					improveResultFactory.putRecordBean(info);
				}
			}
			improveResultFactory.removeRecord(new RecordRemoveDTO(CarbonBaseConstant.供货订单_ghdd,new  RemoveDTO(null,order.getRecordCode(),null)) );
			return false;
		}
		return false;
	}

	@Override
	public void postImprove(FuncGroupContext funcGroupContext, ProRecord proRecord, FGRecordComplexus fgRecordComplexus, ImproveResultFactory improveResultFactory) {

	}
}
